package com.example.good.service.impl;

import com.example.good.entity.User;
import com.example.good.mapper.UserMapper;
import com.example.good.service.IUserService;
import com.example.good.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@Service    //@Service注解:将当前类的对象交给Spring来管理,自动创建对象和对象的维护
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    /**定义一个md5算法加密处理*/
    private String getMD5psw(String password,String salt){
        for (int i=0;i<3;i++){
            password= DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();
        }
        return password;
    }
    @Override
    public void register(User user) {
        String username=user.getUsername();
        User result = userMapper.findByUsername(username);
        if (result != null){
            throw new UsernameDuplicatedException("用户名被占用");
        }
        String BeforeMd5Password = user.getPassword();
        String salt = UUID.randomUUID().toString().toUpperCase();
        String AfterMd5Password = getMD5psw(BeforeMd5Password,salt);
        user.setPassword(AfterMd5Password);
        user.setSalt(salt);
        user.setCreatedUser(user.getUsername());
        user.setCreatedTime(new Date());
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(new Date());
        Integer rows = userMapper.insert(user);
        if (rows != 1){
            throw new InsertException("插入异常");
        }
    }

    @Override
    public User login(String username, String password) {
        User result = userMapper.findByUsername(username);
        if (result == null){
            throw new UserNotFoundException("用户不存在");
        }
        String AfterMd5Password = result.getPassword();
        String salt = result.getSalt();
        String JudgeMd5Password = getMD5psw(password,salt);
        if (!JudgeMd5Password.equals(AfterMd5Password)){
            throw new PasswordNotMatchException("密码错误");
        }
//        return null;
        User user = new User();
        user.setUid(result.getUid());
        user.setUsername(result.getUsername());
        return user;
    }

    @Override
    public void forgetPsw(String username, String phone) {
        User result = userMapper.findByUsername(username);
        if (result == null){
            throw new UserNotFoundException("用户名不存在");
        }
        if (!phone.equals(result.getPhone())){
            throw new PhoneErrorException("用户绑定的手机号不正确");
        }
    }

    @Override
    public User getByUid(Integer uid) {
        User result = userMapper.findByUid(uid);
        if (result == null ){
            throw new UserNotFoundException("用户不存在");
        }
        User user = new User();
        user.setUsername(result.getUsername());
        user.setPhone(result.getPhone());
        user.setEmail(result.getEmail());
        user.setGender(result.getGender());
        return user;
    }

    @Override
    public void checkPassword(Integer uid, String username, String oldPassword) {
        User result = userMapper.findByUid(uid);
        if (result == null){
            throw new UserNotFoundException("用户不存在");
        }
        String JudgeMd5Password = getMD5psw(oldPassword,result.getSalt());
        String AfterMd5Password = result.getPassword();
        if (!JudgeMd5Password.equals(AfterMd5Password)){
            throw new PasswordNotMatchException("密码错误");
        }
    }

    @Override
    public void changePassword(Integer uid, String username, String newPassword) {
        User result = userMapper.findByUid(uid);
        if (result == null){
            throw new UserNotFoundException("用户不存在");
        }
        String ChangedPassword = getMD5psw(newPassword, result.getSalt());
        Integer rows = userMapper.updatePasswordByUid(
                uid, ChangedPassword, username, new Date());
        if (rows != 1){
            throw new UpdateException("更新错误");
        }

    }

    @Override
    public void changeInfo(Integer uid, String username, User user) {
        User result = userMapper.findByUid(uid);
        if (result == null){
            throw new UserNotFoundException("用户不存在");
        }
        user.setUid(uid);
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());
        Integer rows = userMapper.updateInfoByUid(user);
        if (rows != 1){
            throw new UpdateException("更新错误");
        }
    }
}
